package com.inews.business.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inews.base.exception.BusinessException;
import com.inews.base.mapper.BeanMapper;
import com.inews.base.persistence.DynamicSpecifications;
import com.inews.base.persistence.SearchFilter;
import com.inews.base.persistence.SearchFilter.Operator;
import com.inews.base.security.vo.BaseUserVo;
import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.InewsUtils;
import com.inews.base.vo.ExtData;
import com.inews.business.constants.InewsBusinessConstants;
import com.inews.business.entity.TblDegreeStudy;
import com.inews.business.entity.TblLevelChange;
import com.inews.business.entity.TblSysUser;
import com.inews.business.entity.TblTeacher;
import com.inews.business.repository.DegreeStudyDao;
import com.inews.business.repository.ITeacherDao;
import com.inews.business.vo.DegreeStudyVo;
import com.inews.business.vo.LevelChangeVo;
import com.inews.business.vo.TeacherVo;

@Service
@Transactional(readOnly=true)
public class DegreeStudyServiceImpl implements DegreeStudyService, InewsBusinessConstants{

	@Autowired
	private DegreeStudyDao degreeStudyDao;
	@Autowired
	private BaseService baseService;
	@Autowired
	private ITeacherService teacherService;
	
	
	/**
	 * 分页查询教师进修信息.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<DegreeStudyVo> findDegreeStudyPage(Map<String, Object> filterParams, Pageable page, TeacherVo vo){
		List<DegreeStudyVo> voList = new ArrayList<DegreeStudyVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("status", new SearchFilter("status", Operator.EQ, DS_STATUS_NORMAL));
		
		Page<TblDegreeStudy> pagePoList = degreeStudyDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblDegreeStudy.class), page);
		for (TblDegreeStudy po : pagePoList.getContent()) {
			DegreeStudyVo degreeStudyVo = new DegreeStudyVo();
			//添加人员信息
			teacherService.setTeacherInfo(degreeStudyVo, po.getStaffId());
			BeanMapper.copy(po, degreeStudyVo);
			degreeStudyVo.setStartTimeFormat(InewsUtils.fomatDate(po.getStartTime()));
			degreeStudyVo.setEndTimeFormat(InewsUtils.fomatDate(po.getEndTime()));
			vo.setLastUpdUserName(po.getLastUpdUser().getRealName());
			vo.setLastUpdTimeFormat(InewsUtils.fomatLongDate(po.getLastUpdTime()));
			voList.add(degreeStudyVo);
		}
		ExtData<DegreeStudyVo> extData = ExtDataUtil.genWithPagingList(voList, pagePoList.getTotalElements());
		return extData;
	}
	
	
	
	/**
	 * 保存教师进修信息.
	 *
	 * @param vo the vo
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<DegreeStudyVo> saveDegreeStudy(DegreeStudyVo vo, BaseUserVo user){
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblDegreeStudy po = BeanMapper.map(vo, TblDegreeStudy.class);
		po.setStartTime(InewsUtils.fomatDate2String(vo.getStartTimeFormat()));
		po.setEndTime(InewsUtils.fomatDate2String(vo.getEndTimeFormat()));
		po.setStatus(DS_STATUS_NORMAL);
		po.setCreateTime(currentTime);
		po.setCreateUser(editUser);
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_ADD);
		po.setLastUpdTime(currentTime);
		degreeStudyDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}
	
	
	
	/**
	 * 更新教师进修信息.
	 *
	 * @param vo the vo
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<DegreeStudyVo> updateDegreeStudy(DegreeStudyVo vo, BaseUserVo user){
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblDegreeStudy po = degreeStudyDao.findOne(vo.getId());
		po.setStaffName(vo.getStaffName());
		po.setDepartment(vo.getDepartment());
		po.setAge(vo.getAge());
		po.setContent(vo.getContent());
		po.setSchool(vo.getSchool());
		po.setSubject(vo.getSubject());
		po.setStartTime(InewsUtils.fomatDate2String(vo.getStartTimeFormat()));
		po.setEndTime(InewsUtils.fomatDate2String(vo.getEndTimeFormat()));
		po.setRemark(vo.getRemark());
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_UPDATE);
		po.setLastUpdTime(currentTime);
		degreeStudyDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}
	
	
	
	/**
	 * 删除教师进修信息.
	 *
	 * @param vo the vo
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<DegreeStudyVo> deleteDegreeStudy(DegreeStudyVo vo, BaseUserVo user){
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblDegreeStudy po = degreeStudyDao.findOne(vo.getId());
		po.setStatus(RS_STATUS_DELETE);
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_DELETE);
		po.setLastUpdTime(currentTime);
		degreeStudyDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}
	
	
	/**
	 * 导出excel.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<DegreeStudyVo> exportExcel(Map<String, Object> filterParams){
		List<DegreeStudyVo> allDegreeStudyVoList = new ArrayList<DegreeStudyVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("status", new SearchFilter("status", Operator.EQ, DS_STATUS_NORMAL));
		List<TblDegreeStudy> result=	this.degreeStudyDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblDegreeStudy.class));
		
			for (TblDegreeStudy po : result) {
				
				DegreeStudyVo degreeStudyVo = BeanMapper.map(po, DegreeStudyVo.class);
				degreeStudyVo.setStartTimeFormat(InewsUtils.fomatDate(po.getStartTime()));
				degreeStudyVo.setEndTimeFormat(InewsUtils.fomatDate(po.getEndTime()));
			

				allDegreeStudyVoList.add(degreeStudyVo);
			}
		
		return ExtDataUtil.genWithData(allDegreeStudyVoList);
		
	}
	
	
	
	/**
	 * 导入excel.
	 *
	 * @param excelFile the excel file
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<String> importExcel(InputStream is, BaseUserVo user){
		List<DegreeStudyVo> voList = new ArrayList<DegreeStudyVo>();
		try {
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0); 
		    if(InewsUtils.isNotEmpty(hssfSheet)){
		    	//循环行Row(第一行为标题，跳过)   
		    	for(int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++){  
			        HSSFRow hssfRow = hssfSheet.getRow(rowNum);  
			        if(InewsUtils.isEmpty(hssfRow)){  
			          continue;  
			        }  
			        DegreeStudyVo vo = new DegreeStudyVo();
			        String idStr = InewsUtils.getValue(hssfRow.getCell(0));
			        Long id = Long.valueOf(idStr.substring(0, idStr.indexOf(".")));
			        vo.setId(id);
			        vo.setDepartment(InewsUtils.getValue(hssfRow.getCell(1)));
			        vo.setStaffId(InewsUtils.getValue(hssfRow.getCell(2)));
			        vo.setStaffName(InewsUtils.getValue(hssfRow.getCell(3)));
			        vo.setAge(InewsUtils.getValue(hssfRow.getCell(4)));
			        vo.setContent(InewsUtils.getValue(hssfRow.getCell(5)));
			        vo.setSchool(InewsUtils.getValue(hssfRow.getCell(6)));
			        vo.setSubject(InewsUtils.getValue(hssfRow.getCell(7)));
			        vo.setStartTimeFormat(InewsUtils.getValue(hssfRow.getCell(8)));
			        vo.setEndTimeFormat(InewsUtils.getValue(hssfRow.getCell(9)));
			        vo.setRemark(InewsUtils.getValue(hssfRow.getCell(10)));
			        voList.add(vo);
		    	}  
		    }
		    for (DegreeStudyVo vo : voList) {
				if(degreeStudyDao.exists(vo.getId())){
					updateDegreeStudy(vo, user);
				}else{
					saveDegreeStudy(vo, user);
				}
			}
		} catch (IOException e) {
			throw new BusinessException("导入失败");
		}
		return ExtDataUtil.genWithSingleData("导入成功");
	}
}
